package de.fhb.itunes.manager.filewatcher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.ByteChannel;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import de.fhb.itunes.manager.Config;

public class OrdnerUebweachung {
	private Properties prop;
	private String path;
	private ArrayList<String> filme;
	private ArrayList<String> musik;

	public OrdnerUebweachung() {
		Config config = new Config();
		this.prop = config.getProperties();
		this.path = prop.getProperty("order_path");
		System.out.println(this.path);
		filme = new ArrayList<String>();
		musik = new ArrayList<String>();
	}

	public ArrayList<String> getFilmeNames() {
		return filme;
	}

	public ArrayList<String> getMusikNames() {
		return musik;
	}

	public void checkDir() throws IOException {
		File f = new File(this.path);
		// File[] fileArray = f.listFiles();
		File dir = f;
		File[] files = dir.listFiles();
		if (files != null) { // Erforderliche Berechtigungen etc. sind vorhanden
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					// Es ist ein Ordner
				} else {
					// Es ist ein File
					String extension = extension(files[i]);
					if (extension.equalsIgnoreCase("m4v")) {
						filme.add(withoutextension(files[i]));
						copy(files[i].getAbsolutePath(), files[i].getName());

					} else if (extension.equalsIgnoreCase("m4a")
							|| extension.equalsIgnoreCase("mp3")) {
						musik.add(withoutextension(files[i]));
						copy(files[i].getAbsolutePath(), files[i].getName());
					}
				}
			}
		}
	}

	private String extension(File f) {
		int dot = f.getAbsolutePath().lastIndexOf('.');
		return f.getAbsolutePath().substring(dot + 1);
	}

	private String withoutextension(File f) {
		int dot = f.getName().lastIndexOf('.');
		return f.getName().substring(0, dot);
	}

	public void copy(String absPath, String filename) throws IOException {
		String src = absPath;
		String dest = prop.getProperty("order_path_dest");
		
		copy(new File(src),new File(dest + "/"+ filename));
		
		
		/*
		try {
			RandomAccessFile datei = new RandomAccessFile(src, "r");
			RandomAccessFile neudatei = new RandomAccessFile(dest + "/"
					+ filename, "rw");

			while (neudatei.length() < datei.length()) {
				neudatei.write(datei.read());
			}
			datei.close();
			neudatei.close();
		} catch (IOException e) {
			System.err.println(e);
		}
*/
	}

	public static void copy(File source, File destination) {
		try {
			FileInputStream fileInputStream = new FileInputStream(source);
			FileOutputStream fileOutputStream = new FileOutputStream(
					destination);

			FileChannel inputChannel = fileInputStream.getChannel();
			FileChannel outputChannel = fileOutputStream.getChannel();

			transfer(inputChannel, outputChannel, source.length(),
					1024 * 1024 * 32 /* 32 MB */, true, true);

			fileInputStream.close();
			fileOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			source.delete();
		}
	}

	public static void transfer(FileChannel fileChannel,
			ByteChannel byteChannel, long lengthInBytes,
			long chunckSizeInBytes, boolean verbose, boolean fromFile)
			throws IOException {

		long overallBytesTransfered = 0L;
		long time = -System.currentTimeMillis();
		while (overallBytesTransfered < lengthInBytes) {

			long bytesTransfered = 0L;

			if (fromFile) {
				bytesTransfered = fileChannel.transferTo(
						0,
						Math.min(chunckSizeInBytes, lengthInBytes
								- overallBytesTransfered), byteChannel);
			} else {
				bytesTransfered = fileChannel.transferFrom(
						byteChannel,
						overallBytesTransfered,
						Math.min(chunckSizeInBytes, lengthInBytes
								- overallBytesTransfered));
			}

			overallBytesTransfered += bytesTransfered;

			if (verbose) {
				System.out.printf(
						"overall bytes transfered: %s progress %s%%\n",
						overallBytesTransfered,
						Math.round(overallBytesTransfered
								/ ((double) lengthInBytes) * 100.0));
			}

		}
		time += System.currentTimeMillis();

		if (verbose) {
			System.out.printf("Transfered: %s bytes in: %s s -> %s kbytes/s",
					overallBytesTransfered, time / 1000,
					(overallBytesTransfered / 1024.0) / (time / 1000.0));
		}

	}
}
